<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-12-09 13:33:23
 * @Last Modified time: 2023-02-03 10:07:32
 */
namespace app\common\services\store;

use app\common\dao\store\ShippingTemplatesDao;
use app\common\services\store\ShippingFreeService;
use app\common\services\store\ShippingRegionsService;
use app\common\services\RegionService;

use frank\base\BaseService;
use frank\exception\CommonException;
use frank\utils\helper\ArrayHelper;

/**
 * Class ShippingTemplatesService
 */
class ShippingTemplatesService extends BaseService
{

    /**
     * ShippingTemplatesService constructor.
     * @param ShippingTemplatesDao $dao
     */
    public function __construct(ShippingTemplatesDao $dao)
    {
        $this->dao = $dao;
    }

    public function getShippingTemplatesDetail($filter)
    {
        $detail = $this->dao->getDetail($filter);
        if (!$detail) {
            throw new CommonException('数据不存在');
        }
        $detail['citys'] = $this->getRegionData($detail['citys']);
        $detail['free_citys'] = $this->getRegionData($detail['free_citys']);
        return $detail->toArray();
    }

    /**
     * 获取服务列表
     */
    public function getShippingTemplatesList(array $where)
    {
        [$page, $limit] = $this->getPageValue();
        $data = $this->dao->getShippingTemplatesList($where, $page, $limit);
        return $data;
    }

    /**
     * 获取服务数据
     */
    public function getShippingTemplatesSelect(array $where = [])
    {
        $data = $this->dao->getShippingTemplatesSelect($where);
        return $data;
    }

    public function createTemplate(array $data,array $citys,array $freeData){
    	$regionsServive = ShippingRegionsService::instance();
    	$freeServive = ShippingFreeService::instance();
        // 保存数据
        return $this->transaction(function () use ($data, $citys,$regionsServive,$freeData,$freeServive) {
        	$res = $this->dao->create($data);
        	if (!$res) throw new CommonException('保存失败');
        	$id = (int)$res->id;
	        $regionsServive->createExpressRegions($citys,$id);
	        $freeServive->createFreeRegions($freeData,$id);
	        return true;
        });
    }

    public function updateTemplate(int $id,array $data,array $citys,array $freeData){
        $regionsServive = ShippingRegionsService::instance();
        $freeServive = ShippingFreeService::instance();
        // 保存数据
        return $this->transaction(function () use ($id,$data, $citys,$regionsServive,$freeData,$freeServive) {
            $res = $this->dao->update($id,$data);
            if (!$res) throw new CommonException('保存失败');
            $regionsServive->createExpressRegions($citys,$id);
            $freeServive->createFreeRegions($freeData,$id);
            return true;
        });
    }
    public function deleteTemplate(int $id){
        $regionsServive = ShippingRegionsService::instance();
        $freeServive = ShippingFreeService::instance();
        // 保存数据
        return $this->transaction(function () use ($id,$regionsServive,$freeServive) {
            $res = $this->dao->delete($id);
            if (!$res) throw new CommonException('删除失败');
            $regionsServive->deleteExpressRegions($id);
            $freeServive->deleteFreeRegions($id);
            return true;
        });
    }


    /**
     * 获取配送区域及运费设置项
     */
    public function getRegionData($list)
    {
        // 所有地区
        $regions = RegionService::instance()->getCacheAll();
        $data = [];
        foreach ($list as $item) {
            $arr = $item;
            $arr['citys'] = ArrayHelper::objectToArray($item['citys']);
            $province = [];
            foreach ($arr['citys'] as $cityId) {
                if (!isset($regions[$cityId])) continue;
                !in_array($regions[$cityId]['pid'], $province) && $province[] = $regions[$cityId]['pid'];
            }
            $arr['province'] = $province;
            $data[] = $arr;
        }
        return $data;
    }


}